package com.naiterui.ehp.bs.pharmacist.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.bo.esign.DigitalCertificateBO;
import com.naiterui.ehp.bp.bo.esign.EsignNoSecretBO;
import com.naiterui.ehp.bp.bo.esign.NoSecretParamBO;
import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.bo.esign.WillTypeBO;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bp.utils.sms.SMSUtil;
import com.naiterui.ehp.bs.pharmacist.entity.Pharmacist;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.feign.PharmacistEsignFeignClient;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.PharmacistRepository;
import com.naiterui.ehp.bs.pharmacist.service.IEsignUserService;
import com.naiterui.ehp.bs.pharmacist.service.ISMSService;
import com.naiterui.ehp.bs.pharmacist.utils.ChackPasswordUtils;
import com.naiterui.ehp.bs.pharmacist.utils.Constants;
import com.naiterui.ehp.bs.pharmacist.vo.esign.WillTypeVO;

/**
 * @Description 签名用户相关
 * @Author gongbaoqiang
 * @Date 2020/6/9 1:40 PM
 */
@Service
public class PharmacistEsignUserServiceImpl implements IEsignUserService {

    @Autowired
    private PharmacistRepository pharmacistRepository;

    @Autowired
    private PharmacistEsignFeignClient pharmacistEsignFeignClient;

    @Autowired
    private ISMSService smsService;
    @Value("${ehp.domain.img}")
    private String imgDomain;

    @Override
    public void getRealnameCode(Long pharmacistId) throws BusinessException {
        Pharmacist pharmacist = getPharmacist(pharmacistId);
        pharmacistEsignFeignClient.getRealnameCode(pharmacist.getName(), pharmacist.getPhone(), pharmacist.getCardNo());
    }

    @Override
    public void checkRealnameCode(Long pharmacistId, String code) throws BusinessException {
        Pharmacist pharmacist = getPharmacist(pharmacistId);
        //实名验证，实名状态由签名服务维护
        pharmacistEsignFeignClient.checkRealnameByCode(pharmacist.getPhone(), code);
    }

    @Override
    public List<WillTypeVO> getWillTypes(Long pharmacistId) throws BusinessException {
        List<WillTypeBO> bos = pharmacistEsignFeignClient.getWillTypes(pharmacistId);
        return BeanUtil.copyList(bos, new ArrayList<>(), WillTypeVO.class);
    }

    @Override
    public void updateWillType(Long pharmacistId, String willType) throws BusinessException {
        pharmacistEsignFeignClient.updateWillType(pharmacistId, willType);
    }

    @Override
    public void updatePassword(Long pharmacistId, String passwd, String code) throws BusinessException {
        Pharmacist pharmacist = getPharmacist(pharmacistId);
        //校验验证码码
        String redisKey = Constants.PHONE_ESIGN_PWD_KEY + pharmacist.getPhone();
        SMSUtil.validCode(code, redisKey,  CommonConstant.REDIS_PRE_PHARMACIST);

        //密码解密
        String pwd = ChackPasswordUtils.decryptPasswd(passwd);

        pharmacistEsignFeignClient.updatePasswd(pharmacistId, pwd);
        //缓存医生设置密码状态
        String statusKey = Constants.PHARMACIST_ESIGN_PWD_SET_KEY + pharmacistId;
        RedisUtil.valueOps().set(statusKey, true);
    }

    @Override
    public Boolean passwordStatus(Long pharmacistId) throws BusinessException{
        String statusKey = Constants.PHARMACIST_ESIGN_PWD_SET_KEY + pharmacistId;
        return RedisUtil.valueOps().getObject(statusKey) == null ? false : true;
    }

    @Override
    public EsignNoSecretBO getNoSecretByPharmacistId(Long pharmacistId) throws BusinessException {
        Integer noSecret = pharmacistEsignFeignClient.getNoSecret(pharmacistId);
        EsignNoSecretBO noSecretBO = new EsignNoSecretBO();
        if(noSecret == 0){
            noSecretBO.setNoSecret(false);
        }else {
            noSecretBO.setNoSecret(true);
            noSecretBO.setNotRemind(false);
            return noSecretBO;
        }
        boolean existsKey = RedisUtil.keyOps().existsKey(CommonConstant.PH_NO_SECRET_REMIND + pharmacistId);
        if(existsKey){
            noSecretBO.setNotRemind(false);
        }else {
            noSecretBO.setNotRemind(true);
        }
        return noSecretBO;
    }

    @Override
    public PreSignResultBO preNoSecret(Long pharmacistId) throws BusinessException {
        return pharmacistEsignFeignClient.preNoSecret(pharmacistId);
    }

    @Override
    public void setNoSecret(Long pharmacistId, Boolean noSecret, String authPwd, String uniqueId)
        throws BusinessException {
        pharmacistEsignFeignClient.setNoSecret(NoSecretParamBO.builder().signUserId(pharmacistId).noSecret(noSecret)
            .authPwd(authPwd).uniqueId(uniqueId).build());;
    }

    @Override
    public DigitalCertificateBO getDigitalCertificate(Long pharmacistId) throws BusinessException {
        return pharmacistEsignFeignClient.getDigitalCertificate(pharmacistId);
    }

    @Override
    public String signLicense(Long pharmacistId) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }
        return StringUtils.isNotEmpty(pharmacist.getSealImage()) ? imgDomain + pharmacist.getSealImage() : "";
    }

    private Pharmacist getPharmacist(Long pharmacistId) throws BusinessException{
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.PRESCRIPTION_SIGN_DOCTOR_NOT_RECORD_ERROR);
        }
        return pharmacist;
    }
}
